﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutomaticCodeGeneration
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            btnOk.Enabled = false;
            StringBuilder sb = new StringBuilder();
            progressBar_status.Maximum = int.Parse(tb_number.Text);
            lbl_status.Text = "请耐心等待...";
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                string folderPath = folderBrowserDialog1.SelectedPath;
                string txtPathName = "code.txt";
                string txtPaht = Path.Combine(folderPath, txtPathName);
                int r = 0;
                string tempFile = "";
                try
                {
                    for (int i = 1; i <= int.Parse(tb_number.Text); i++)
                    {

                        #region 生成编码
                        string code = DateTime.Now.ToString("ddHHmmssffffff");
                        sb.Append(tb_prefix.Text + code + tb_suffix.Text + "\r\n");
                        Thread.Sleep(1);
                        #endregion

                        if (i==1)
                        {
                            tempFile = code;
                        }
                        #region 满1000条保存一次文件
                        if (i >= 1000 && i % 1000 == 0)
                        {
                            txtPathName = $"{tempFile}_{i / 1000}.txt";
                            txtPaht = Path.Combine(folderPath, txtPathName);
                            #region 编码写入文件
                            using (FileStream fs = new FileStream(txtPaht, FileMode.Create))
                            {
                                using (StreamWriter sw = new StreamWriter(fs))
                                {
                                    sb.Remove(0, 14+tb_prefix.Text.Trim().Length+tb_suffix.Text.Trim().Length);
                                    //开始写入
                                    sw.Write(sb.ToString());
                                    //清空缓冲区
                                    sw.Flush();
                                    //清空
                                    sb.Clear();
                                }
                            }
                            progressBar_status.Value = i;
                            #endregion
                        }
                        #endregion
                        r = i / 1000 + 1;
                    }
                    #region 编码写入文件
                    //using (FileStream fs = new FileStream(Path.Combine(folderPath, $"code{r}.txt"), FileMode.Create))
                    using (FileStream fs = new FileStream(Path.Combine(folderPath, $"{tempFile}.txt"), FileMode.Create))
                    {
                        using (StreamWriter sw = new StreamWriter(fs))
                        {
                            sb.Remove(0, 14 + tb_prefix.Text.Trim().Length + tb_suffix.Text.Trim().Length);
                            //开始写入
                            sw.Write(sb.ToString());
                            //清空缓冲区
                            sw.Flush();
                        }
                    }
                    progressBar_status.Value = int.Parse(tb_number.Text);
                    lbl_status.Text = "全部生成成功";
                    #endregion

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                DialogResult dr = MessageBox.Show("全部生成成功", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                if (dr == DialogResult.OK)
                {
                    lbl_status.Text = "";
                    progressBar_status.Value = 0;
                }
            }
            btnOk.Enabled = true;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            lbl_status.Text = "";
        }
    }
}
